/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.simulator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;

import ch.bfh.ti.kybernetik.simulator.controller.lightBulb.LightBulbController;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RoboterController;
import ch.bfh.ti.kybernetik.simulator.controller.roboter.RoboterMoveStateFactory;

public class Simulator extends Observable implements Runnable {

	private List<LightBulbController> lightBulbControllers = null;

	private List<RoboterController> roboterControllers = null;

	private int height = 1;

	private int width = 500;

	private boolean simulating = false;

	private boolean running = true;

	private volatile int simulatorSpeed = 10;

	public Simulator(LightBulbController[] lightBulbController, RoboterController... roboterController) {
		this.roboterControllers = Collections.synchronizedList(new ArrayList<RoboterController>(Arrays.asList(roboterController)));
		this.lightBulbControllers = Collections.synchronizedList(new ArrayList<LightBulbController>(Arrays.asList(lightBulbController)));
	}

	@Override
	public void run() {
		while (running) {

			Thread.yield();

			while (simulating) {
				try {

					Thread.sleep(simulatorSpeed);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.iterateRoboterControllers(new ThreadSafeIterationAction<RoboterController>() {
					@Override
					public void performAction(RoboterController roboterController) {
						// System.out.println("Simulator: Iterate RoboterController");
						int leftSensorIntensity = 0;
						int rightSensorIntensity = 0;
						double robotXPos = roboterController.getRoboter().getX();
						double robotYPos = roboterController.getRoboter().getY();
						for (LightBulbController lightBulbController : lightBulbControllers) {
							if (lightBulbController.isWithinRadius((int) robotXPos, (int) robotYPos)) {
								leftSensorIntensity = leftSensorIntensity
										+ lightBulbController.calculateIntensity(roboterController.getLeftLightSensorXPos(),
												roboterController.getLeftLightSensorYPos());
								rightSensorIntensity = rightSensorIntensity
										+ lightBulbController.calculateIntensity(roboterController.getRightLightSensorXPos(),
												roboterController.getRightLightSensorYPos());
							}
						}
						roboterController.calculateNewRoboterPosition(leftSensorIntensity, rightSensorIntensity);

					}
				});

				this.setChanged();
				this.notifyObservers(SimulatorCommand.MOVED);

			}
		}

	}

	private boolean roboterOutOfSimulation() {

		return false;
	}

	public boolean isSimulating() {
		return simulating;
	}

	public void setSimulating(boolean simulating) {
		this.simulating = simulating;
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

	public int getSimulatorSpeed() {
		return simulatorSpeed;
	}

	public void setSimulatorSpeed(int simulatorSpeed) {
		this.simulatorSpeed = simulatorSpeed;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getWidth() {
		return width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	public boolean addRoboterController(RoboterController roboterController) {
		return this.getRoboterControllers().add(roboterController);
	}

	public void iterateRoboterControllers(ThreadSafeIterationAction<RoboterController> action) {
		Collection<RoboterController> list = getRoboterControllers();
		synchronized (list) {
			final Iterator<RoboterController> i = list.iterator();
			while (i.hasNext()) {
				action.performAction(i.next());
			}
		}
	}

	private List<RoboterController> getRoboterControllers() {
		return roboterControllers;
	}

	public void setDeltaT(double deltaT) {
		RoboterMoveStateFactory.getInstance().setDeltaT(deltaT);
	}

	public double getDeltaT() {
		return RoboterMoveStateFactory.getInstance().getDeltaT();
	}

}
